package com.etooling.framework.service.impl;

import com.etooling.framework.entity.AttachFile;
import com.etooling.framework.entity.FileResource;
import com.etooling.framework.repository.AttachFileRepository;
import com.etooling.framework.service.FileUploadSaveService;
import com.etooling.framework.utils.DateFm;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author X
 * @title: FileUploadSaveServiceImpl
 * @projectName irobot_jjb
 * @description: 上传到应用服务器的应用
 * @date 2020/12/2917:39
 */
public class FileUploadToServerServiceImpl implements FileUploadSaveService {

    @Override
    public AttachFile upload(AttachFileRepository attachFileRepository, MultipartFile uploadFile, FileResource fileResource, Boolean isEncrypt) throws Exception {
        AttachFile attachFile = new AttachFile();
        attachFile.setEncrypt(isEncrypt);
        //原来的文件名
        String origName = uploadFile.getOriginalFilename();
        String fileType = origName.substring(origName.lastIndexOf("."));
        //为了文件的安全进行重命名
        String newFileName = UUID.randomUUID().toString() + fileType;
        //拿出服务器存放目录
        attachFile.setName(origName);
        attachFile.setFileType(fileType);
        attachFile.setFileResource(fileResource);
        String dataFileRootDir = fileResource.getBucket();
        String fileDir = DateFm.to_char(new Date(), "yyyyMMdd");
        //处理相对路径
        String absName = fileDir + "/" + newFileName;

        //加上日期和根目录的绝对路径
        String filePath = dataFileRootDir + absName;
        filePath = filePath.replaceAll("\\\\", "/");

        File file = new File(filePath);
        File path = file.getParentFile();
        if (!path.exists()) {
            path.mkdirs();
        }
        RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
        InputStream is = uploadFile.getInputStream();
        long fileSize = 0;
        byte[] data = new byte[10240];
        int c = is.read(data);
        fileSize += c;
        while (c > 0) {
            randomFile.write(data, 0, c);
            c = is.read(data);
            fileSize += c;
        }
        randomFile.close();
        attachFile.setFilePath(newFileName);
        attachFile.setFileSize(fileSize);
        //拼接web的路径
        String webPath = fileResource.getBasePath() + absName;
        attachFile.setFilePath(absName);
        attachFile.setCreateTime(new Date());
        attachFile.setWebPath(webPath);
        System.out.println(attachFile.toString());
        attachFileRepository.save(attachFile);
        return attachFile;
    }

    @Override
    public AttachFile upload(AttachFileRepository attachFileRepository, MultipartFile uploadFile, FileResource fileResource, Boolean isEncrypt, Map<String, String> limitMap) throws Exception {
        AttachFile attachFile = new AttachFile();
        attachFile.setEncrypt(isEncrypt);
        //原来的文件名
        String origName = uploadFile.getOriginalFilename();
        String fileType = origName.substring(origName.lastIndexOf("."));
        //为了文件的安全进行重命名
        String newFileName = UUID.randomUUID().toString() + fileType;
        //拿出服务器存放目录
        attachFile.setName(origName);
        if (limitMap.get(origName) != null) {
            attachFile.setFileType(fileType);
            attachFile.setFileResource(fileResource);
            String dataFileRootDir = fileResource.getBucket();
            String fileDir = DateFm.to_char(new Date(), "yyyyMMdd");
            //处理相对路径
            String absName = fileDir + "/" + newFileName;

            //加上日期和根目录的绝对路径
            String filePath = dataFileRootDir + absName;
            filePath = filePath.replaceAll("\\\\", "/");

            File file = new File(filePath);
            File path = file.getParentFile();
            if (!path.exists()) {
                path.mkdirs();
            }
            RandomAccessFile randomFile = new RandomAccessFile(file, "rw");
            InputStream is = uploadFile.getInputStream();
            long fileSize = 0;
            byte[] data = new byte[10240];
            int c = is.read(data);
            fileSize += c;
            while (c > 0) {
                randomFile.write(data, 0, c);
                c = is.read(data);
                fileSize += c;
            }
            randomFile.close();
            attachFile.setFilePath(newFileName);
            attachFile.setFileSize(fileSize);
            //拼接web的路径
            String webPath = fileResource.getBasePath() + absName;
            attachFile.setFilePath(absName);
            attachFile.setCreateTime(new Date());
            attachFile.setWebPath(webPath);
            System.out.println(attachFile.toString());
            attachFileRepository.save(attachFile);
            return attachFile;
        } else {
            return null;
        }
    }

    @Override
    public AttachFile uploadByte(AttachFileRepository attachFileRepository, String fileName, byte[] image, FileResource fileResource, Boolean isEncrypt) throws Exception {
        return null;
    }

    @Override
    public AttachFile uploadXinDi(AttachFileRepository attachFileRepository, MultipartFile uploadFile, FileResource fileResource, Boolean isEncrypt) throws Exception {
        return null;
    }

}
